/*
 * 创建日期 2005-9-30
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.view;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.element.Element;

/**
 * @author wkf5985
 *
 * 
 */
public class CopyElement extends Element{

 	//被拷贝的行，起始行和终止行有","号分隔，例如：1,9 ,表示第1行到第9行，并且包括第一行和第9行
 	private String srcrow;
 	private int dest_start_row;
 	//相对于currentrow的偏移量
 	private int offset;
 	
	public void validator() throws ReportException{
		Validation.checkRequired("CopyElement","srcrow",srcrow);
	}
	
	/**
	 * 标签执行方法，递归执行
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		try{
			int index = srcrow.indexOf(",");
			if(index <= 0){
				return;
			}
			int start = Integer.parseInt(srcrow.substring(0,index))-1;
			int end = Integer.parseInt(srcrow.substring(index+1))-1;
			//如果没有设置目标起始行，则默认为当前行
			int dest = dest_start_row;
			if(dest <= 0){
				dest = reportData.getCurrentRow()+1+offset;
			}
			
			ExcelUtils.copyRows(reportData.getFormatSheet(),reportData.getReportSheet(),start,end,dest-1);
			
			//设置当前行数
			reportData.setCurrentRow(end-start+dest-offset);

			super.execute(obj);
			
			//System.out.println("copy currentRow = "+reportData.getCurrentRow());
		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}
	}

	/**
	 * @return
	 */
	public int getDest_start_row() {
		return dest_start_row;
	}

	/**
	 * @return
	 */
	public String getSrcrow() {
		return srcrow;
	}

	/**
	 * @param i
	 */
	public void setDest_start_row(int i) {
		dest_start_row = i;
	}

	/**
	 * @param string
	 */
	public void setSrcrow(String string) {
		srcrow = string;
	}

	/**
	 * @return
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * @param i
	 */
	public void setOffset(int i) {
		offset = i;
	}

}
